﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace rand_exam_check
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        double a, b;//2个操作数
        bool se=true;
        string op;  //运算符
        double result;//答案
        int score = 0;//得分
        int count = 0;//题目序号
        int down_count = 50;//总时间50秒
        int down = 5;//每题时间5秒
        const int sum = 10;//题量
        
        //出题
        private void question()
        {
            Random rnd = new Random();
            a = rnd.Next(9) + 1;
            b = rnd.Next(9) + 1;

            int c = rnd.Next(2);//随机生成+、-算法的题目
            switch (c)
            {
                case 0: op = "+"; result = a + b; break;
                case 1: op = "-"; result = a - b; break;
            }
            label1.Text = "" + a + "" + op + "" + b + " =";
            textBox1.Text = "";
        }
        //结束判断
        private void nextques()
        {
            if (count < sum)
            {
                textBox1.Text = "";
                question();
                int idx = count + 1;
                label8.Text = idx.ToString() + "：";
                down = 5;
            }
            else
            {
                listBox1.Items.Add("你已答完所有题目！本轮得分为：" + score.ToString());
                button1.Text = "开始答题";
                timer2.Enabled = false;
                timer2.Stop();
                timer1.Enabled = false;
                timer1.Stop();
                label4.Text = "上轮分数：";
                label5.Text = "50秒";
                label1.Text = "     ";
                label8.Text = "1：";
                se = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //开始或结束答题
        private void button1_Click(object sender, EventArgs e)
        {
            timer2.Interval = 1000;//倒计时间隔时间为1秒
            timer1.Interval = 1000;
            if (se)
            {
                down_count = 50;
                button1.Text = "结束答题";
                timer2.Enabled = true;
                timer2.Start();
                timer1.Enabled = true;
                timer1.Start();
                question();
                count = 0;
                score = 0;
                label6.Text = "0";
                listBox1.Items.Clear();
                se = false;
            }
            else
            {
                listBox1.Items.Add("你提前结束答题！本轮得分为：" + score.ToString());
                button1.Text = "开始答题";
                timer2.Enabled = false;
                timer2.Stop();
                timer1.Enabled = false;
                timer1.Stop();
                label4.Text = "上轮分数：";
                label5.Text = "50秒";
                label1.Text = "     ";
                label8.Text = "1：";
                down = 5;
                label10.Text = down.ToString() + "秒";
                se = true;
            }
        }


        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (se)
            {
                listBox1.Items.Add("请先开始答题！！");
                return;
            }
            string input = textBox1.Text;
            int f;

            if (count < sum)
            {
                if (input == "")
                {
                    string str = "请输入答案！";
                    listBox1.Items.Add(str);
                    return;
                }
                else
                {
                    f = int.Parse(input);
                    string disp = "" + a + op + b + " = " + input + "";

                    //检查答案
                    if (result == f)
                    {
                        score += 10;
                        disp += "回答正确!!0.0";
                        listBox1.Items.Add(disp);
                    }
                    else
                    {
                        disp += "回答错误!!TUT";
                        listBox1.Items.Add(disp);
                    }
                }
                label6.Text = score.ToString() + "分";
                count++;
            }
            nextques();
        }

        //多余
        private void label2_Click(object sender, EventArgs e)
        {

        }
        //每题计时器
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (down == 0)
            {
                string disp = "" + a + op + b + " = " + result + "";
                listBox1.Items.Add(disp + "本题超时啦！");
                count++;
                nextques();
                /*textBox1.Text = "";
                 down = 5;
                count++;
                int idx = count + 1;
                label8.Text = idx.ToString() + "：";
                question();*/
            }
            else
            {
                label10.Text = down.ToString() + "秒";
                down--;
            }
        }

        //总计时器
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (down_count == 0)
            {
                button1.Text = "开始答题";
                timer2.Enabled = false;
                timer2.Stop();
                timer1.Enabled = false;
                timer1.Stop();
                label4.Text = "上轮分数：";
                label1.Text = "     ";
                se = true;
                label5.Text= "50秒";
                listBox1.Items.Add("答题时间到！本轮得分为：" + score.ToString());
            }
            else
            {
                label5.Text = down_count.ToString() + "秒";
                down_count--;
            }
        }
    }
}
